﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * 
http://www.spoj.pl/problems/PRIME1/

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5
 */
namespace CSharpAlgorithm
{
    class PrimeGenerator2
    {
        public PrimeGenerator2()
        {
            List<int> basic = Primes.getPrimes(100000);
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int a = Int32.Parse(input[0]);
                int b = Int32.Parse(input[1]);
                byte[] candidateArray = new byte[b - a + 1];
                List<int> candidate = Cutting(candidateArray, basic, a);
                if (candidate[0] == 1)
                {
                    candidate.RemoveAt(0);
                }
                for (int i = 0; i < candidate.Count; i++)
                {
                    Console.WriteLine(candidate[i]);
                }
                Console.WriteLine("");
            }
        }
        private bool CheckPrime(int prime, int max)
        {
            return false;
        }
        private List<int> Cutting(byte[] candidateArray, List<int> basic, int start)
        {
            for (int i = 0; i < basic.Count; i++)
            {
                int prime = basic[i];
                int startIndex;
                if (start > prime)
                {
                    startIndex = start - (start % prime) - start;
                }
                else
                {
                    startIndex = prime - start + prime;
                }
                if (startIndex >= candidateArray.Length)
                {
                    break;
                }
                if (startIndex < 0)
                {
                    startIndex += prime;
                }

                for (int j = startIndex; j < candidateArray.Length; j += prime)
                {
                    candidateArray[j] = 1;
                }
            }
            List<int> candidate = new List<int>();
            for (int i = 0; i < candidateArray.Length; i++)
            {
                if (candidateArray[i] == 0)
                {
                    candidate.Add(start + i);
                }
            }
            return candidate;
        }
    }
}
